package com.wjpeng.wms.web.controller;

import com.wjpeng.wms.domain.Brand;
import com.wjpeng.wms.domain.Product;
import com.wjpeng.wms.query.ProductQueryObject;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IBrandService;
import com.wjpeng.wms.service.IProductService;
import com.wjpeng.wms.util.JSONResult;
import com.wjpeng.wms.util.RequiredPermission;
import com.wjpeng.wms.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    //网站的上下文
    @Autowired
    private ServletContext ctx;

    @Autowired
    private IBrandService brandService;

    @RequestMapping("list")
    @RequiredPermission("部门列表")
    public String list(Model model, @ModelAttribute("qo") ProductQueryObject qo){
        model.addAttribute("result",productService.result(qo));
        model.addAttribute("brands",brandService.list());
        return "product/list";
    }

    @RequestMapping("productListView")
    @RequiredPermission("部门列表")
    public String productListView(Model model, @ModelAttribute("qo") ProductQueryObject qo){
        qo.setPageSize(20);
        model.addAttribute("result",productService.result(qo));
        model.addAttribute("brands",brandService.list());
        return "product/productListView";
    }

    @RequestMapping("input")
    @RequiredPermission("部门编辑")
    public String input(Model model,Long id){
        if(id != null){
            model.addAttribute("entity",productService.get(id));
        }
        model.addAttribute("brands",brandService.list());

        return "product/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiredPermission("部门删除")
    public Object delete(Long id,String imagePath){
        if(id != null){
            if(StringUtils.hasLength(imagePath)){
                UploadUtil.deleteFile(ctx,imagePath);
            }
            productService.delete(id);
        }
        return new JSONResult();
    }



    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiredPermission("部门新增/更新")
    public Object saveOrUpdate(Product product, MultipartFile pic) throws  Exception{
        //处理上传过来的文件
        if(pic != null && pic.getSize() > 0 && pic.getContentType().startsWith("image/")){
            //删除该对象旧的图片
            if(StringUtils.hasLength(product.getImagePath())){
                UploadUtil.deleteFile(ctx,product.getImagePath());
            }
            //把文件保存到服务器中
            String dir = ctx.getRealPath("/upload");
            String imgPath = UploadUtil.upload(pic, dir);//获取存放到服务器里图片的路径
            //把图片服务器路径保存到对象中
            product.setImagePath(imgPath);
        }

        //查询品牌对应的名称
        Brand brand = brandService.get(product.getBrandId());
        product.setBrandName(brand.getName());
        productService.saveOrUpdate(product);
        return new JSONResult();
    }




}
